package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.ColumnInfoQueryDTO;
import com.jeff.tianti.cms.entity.AwardDescription;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.service.AwardDescriptionService;

import javax.servlet.http.HttpServletRequest;

import com.jeff.tianti.cms.service.ColumnInfoService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.cms.dto.AwardDescriptionQueryDTO;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author NOW_GO
 * @desc AwardDescriptionController
 * @date 2019-07-15
 */
@Controller
@RequestMapping("/channel/awardDescription")
public class AwardDescriptionController {

    @Autowired
    private AwardDescriptionService awardDescriptionService;
    @Autowired
    ColumnInfoService columnInfoService;
    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        AwardDescriptionQueryDTO awardDescriptionQueryDTO = new AwardDescriptionQueryDTO();
        awardDescriptionQueryDTO.setCurrentPage(currentPage);
        awardDescriptionQueryDTO.setPageSize(pageSize);
        PageModel<AwardDescription> page = this.awardDescriptionService.queryAwardDescriptionPage(awardDescriptionQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("awardDescriptionQueryDTO", awardDescriptionQueryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MESSAGE_AWARD_DESCRIPTION);

        return "/channel/awardDescription/list";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        AwardDescription awardDescription = null;
        if (StringUtils.isNotBlank(id)) {
            awardDescription = this.awardDescriptionService.find(id);
        }


        ColumnInfoQueryDTO columnInfoQueryDTO = new ColumnInfoQueryDTO();
        columnInfoQueryDTO.setCode(Constants.CODE_ZYJX);
        columnInfoQueryDTO.setIsCodeLike(false);
        List<ColumnInfo> columnInfos = columnInfoService.queryColumnInfoList(columnInfoQueryDTO);
        List<ColumnInfo> awardDescriptionList = null;

        if (!CollectionUtils.isEmpty(columnInfos)) {
            ColumnInfoQueryDTO parentColumnInfoQueryDTO = new ColumnInfoQueryDTO();
            parentColumnInfoQueryDTO.setIsRootColumnLike(false);
            parentColumnInfoQueryDTO.setRootColumnId(columnInfos.get(0).getId());
            awardDescriptionList = columnInfoService.queryColumnInfoList(parentColumnInfoQueryDTO);
        }

        model.addAttribute("awardDescription", awardDescription);
        model.addAttribute("id", id);
        model.addAttribute("awardDescriptionList", awardDescriptionList);

        return "/channel/awardDescription/edit";
    }

    @RequestMapping("/saveorupdate")
    @ResponseBody
    public AjaxResult doSaveOrUpdate(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String description = request.getParameter("description");
            AwardDescription awardDescription = null;
            if (StringUtils.isNotBlank(id)) {
                awardDescription = this.awardDescriptionService.find(id);
                awardDescription.setUpdateDate(new Date());
                awardDescription.setDescription(description);
                awardDescription.setName(name);
                this.awardDescriptionService.update(awardDescription);
            } else {
                awardDescription = new AwardDescription();
                awardDescription.setCreateDate(new Date());
                awardDescription.setDescription(description);
                awardDescription.setName(name);
                this.awardDescriptionService.save(awardDescription);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            AwardDescription awardDescription = this.awardDescriptionService.find(id);
            if (awardDescription != null) {
                awardDescription.setDeleteFlag("1");
                this.awardDescriptionService.update(awardDescription);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }


}